package testjava;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;

public class UrlClient {

	public static void testGet_http(URLConnection urlc) throws IOException {
		HttpURLConnection httpuc = (HttpURLConnection) urlc;
		// Read all the text returned by the server
		urlc.setDoOutput(true);
		urlc.connect();
		InputStream is = urlc.getInputStream();
		byte[] buffer = new byte[1024 * 1024];
		is.read(buffer);
		BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
		String str;
		while ((str = in.readLine()) != null) {
			// str is one line of text; readLine() strips the newline
			// character(s)
			if (str.indexOf("��ǰ") > -1) {
				System.out.println(str);
			}
		}
		// Get the image
		java.awt.Image image = java.awt.Toolkit.getDefaultToolkit().getDefaultToolkit()
				.createImage(urlc.getURL());
		System.out.println(image);
		System.out.println(urlc.getAllowUserInteraction());
		System.out.println(urlc.getDefaultUseCaches());
		System.out.println(urlc.getDoInput());
		System.out.println(urlc.getDoOutput());
		System.out.println(httpuc.getRequestMethod());
		System.out.println(urlc.getInputStream());
		in.close();
	}
	private static void testPOST(URLConnection urlc) throws IOException {
		HttpURLConnection httpuc = (HttpURLConnection) urlc;
		// Read all the text returned by the server
		urlc.setDoOutput(true);
		httpuc.setRequestMethod("POST");
		urlc.connect();
		System.out.println(urlc.getAllowUserInteraction());
		System.out.println(urlc.getDefaultUseCaches());
		System.out.println(urlc.getDoInput());
		System.out.println(urlc.getDoOutput());
		System.out.println(httpuc.getRequestMethod());
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(urlc
				.getOutputStream()));
		out
				.write("POST /usboss42/users/cfgSaleAgentAction.do?reqCode=add HTTP/1.1");
		out.flush();
		out.close();
		System.out.println(httpuc.getResponseCode());
	}
	public static URLConnection getUrlConnection(String urlStr, boolean useProxy)
			throws MalformedURLException, IOException {
		URL url = new URL(urlStr);
		URLConnection urlc = null;
		if (useProxy) {
			SocketAddress sa = new InetSocketAddress("global.proxy.lucent.com", 8000);
			Proxy proxy = new Proxy(Type.HTTP, sa);
			urlc = url.openConnection(proxy);
		} else {
			urlc = url.openConnection();
		}
		// System.out.println(url.getAuthority());
		// System.out.println(url.getQuery());
		// System.out.println(url.getRef());
		// System.out.println(url.getUserInfo());
		return urlc;
	}

	public static void main(String[] args) {
		try {
			String urlStr = "http://59.151.18.18:80/1971233";
			URLConnection urlc = getUrlConnection(urlStr, false);
			testGet_http(urlc);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
